import { defineStore } from 'pinia'
import feedback from '@/utils/feedback'
import {
	ElMessage,
	ElMessageBox,
	ElNotification,
	ElLoading,
	type ElMessageBoxOptions
} from 'element-plus'
interface AppSate {
	tableList : any,
	warehouse_id : any,
	popupTitle : any,
	disabledType : any,
	MouseShow : any,
	typeing : any,
	indexmao : any,
	numshow : any,
	clearSort : any,
}

const useAppStore = defineStore({
	id: 'table',
	state: () : AppSate => {
		return {
			tableList: [],
			warehouse_id: 0,
			popupTitle: '',
			disabledType: false,
			MouseShow: false,
			typeing: '入库单',
			indexmao: null,
			numshow: false,
			clearSort: true,
		}
	},
	actions: {
		onclearSort(index : any) {
			this.clearSort = index
		},
		onnumshow(index : any) {
			this.numshow = index
		},
		ontypeing(index : any) {
			this.typeing = index
		},
		onMouseShow(index : any) {
			this.MouseShow = index
		},
		ondisabledType(index : any) {
			this.disabledType = index
		},
		onpopupTitle(index : any) {
			this.popupTitle = index
		},
		onwarehouse_id(index : any) {
			this.warehouse_id = index
		},
		offwarehouse_id(index : any) {
			this.warehouse_id = ''
		},
		tableListdatas(index : any) {
			this.tableList[index].title = ''
		},
		tableListdata(value : any) {
			this.tableList = []
			this.tableList = value
		},
		tableListAdd(value : any, index : any) {
			this.tableList.splice(index.index + 1, 0, value)
		},
		filterObjs(objcArray : any) {
			for (var i = 0; i < objcArray.length; i++) {
				for (var j = i + 1; j < objcArray.length;) {
					if (`${objcArray[i].product_id}${objcArray[i].category_id}${objcArray[i].unit_length}` == `${objcArray[j].product_id}${objcArray[j].category_id}${objcArray[j].unit_length}`) {
						this.showsing(objcArray)
					} else {
						j++
					}
				}
			}
			return objcArray
		},
		showsing(objcArray : any) {
			ElMessageBox.confirm(
				'此物料已重复添加',
				'提示',
				{
					confirmButtonText: '确认',
					showCancelButton: false,
					showClose: false,
					closeOnClickModal: false,
					closeOnPressEscape: false,
					type: 'warning',
				}
			).then(() => {
				feedback.msgError('物料重复添加请重新选择')
				this.filterObj(objcArray)
			})
			throw new Error() //抛出错误是有用的为了强制停止

		},
		filterObj(objcArray : any) {
			for (var i = 0; i < objcArray.length; i++) {
				for (var j = i + 1; j < objcArray.length;) {
					if (`${objcArray[i].product_id}${objcArray[i].category_id}${objcArray[i].unit_length}` == `${objcArray[j].product_id}${objcArray[j].category_id}${objcArray[j].unit_length}`) {
						feedback.msgError('物料重复添加请重新选择')
						if (j > i) {
							this.indexmao = i
						} else {
							this.indexmao = j
						}
						objcArray.splice(j, 1)
						const a = Math.floor(Math.random() * (100 - 1) + 1)
						const b = Math.floor(Math.random() * (100 - 1) + 1)
						objcArray.push({
							product_id: `${a + b}s`,
							title: '',
							quantity: 0,
							total_price: 0,
							cargo_weight: 0,
							unit_length: 0
						})
					} else {
						j++
					}
				}
			}
			return objcArray
		},
		tableListAddsings(value : any, index : any) {
			this.tableList = this.filterObj(this.tableList)
		},

		tableListAddsing(value : any, index : any) {
			for (let i = 0; i < value.length; i++) {
				if (value[i].category_id == 4) {
					value[i].quantity = 1
					value[i].disabled = true
					value[i].unit_length = 0
				} else {
					value[i].quantity = 0
					value[i].unit_length = 0
				}
				if (!value[i].quantity) {
					value[i].quantity = 0
					value[i].unit_length = 0
					if (value[i].category_id == 4) {
						value[i].quantity = 1
						value[i].disabled = true
					}
				}
				if (!value[i].total_price) {
					value[i].total_price = 0
				}
				if (!value[i].cargo_weight) {
					value[i].cargo_weight = 0
				}

				this.tableList.splice(index++, 1, value[i])
				this.tableList = this.filterObj(this.tableList)

			}
		},
		tableListDel(index : number) {
			this.tableList.splice(index, 1)
		},
	}
})

export default useAppStore